package com.tt.controller;

import com.tt.common.page.QueryInfo;
import com.tt.common.page.TtResult;
import com.tt.pojo.ItemParam;
import com.tt.pojo.vo.CatVo;
import com.tt.service.IGoodsCatService;
import com.tt.service.ItemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 类目下的商品规格
 * @author 青青
 */
@RequestMapping("/itemParam")
@Controller
public class ItemParamController
{
    private String prefix = "param";

    @Autowired
    private ItemParamService itemParamService;

    @Autowired
    private IGoodsCatService goodsCatService;

    @GetMapping("/itemParamPage")
    public String itemParamPage(){return prefix + "/param";}

    @GetMapping("/addItemParam")
    public String addItemParam(){return prefix + "/add-param";}

    @GetMapping("/editItemParam")
    public String editItemParam(Model model,Integer id){

        ItemParam param = itemParamService.getItemParamById(id);
        CatVo catVo = goodsCatService.getCatVo(param.getItemCatId());
        model.addAttribute("itemParam",param);
        model.addAttribute("catVo",catVo);
        return prefix + "/edit-param";
    }


    @PostMapping("/list")
    @ResponseBody
    public TtResult getItemParamList(QueryInfo queryInfo)
    {
        TtResult itemParamList = itemParamService.getItemParamList(queryInfo);
        return itemParamList;
    }

    @RequestMapping("/getItemParam")
    @ResponseBody
    public ItemParam getItemParam(Integer cid){
        ItemParam paramByCid = itemParamService.getParamByCid(cid);
        return paramByCid;
    }


    @PostMapping("/addParam")
    @ResponseBody
    public TtResult addParam(ItemParam itemParam)
    {
        TtResult result = itemParamService.addParam(itemParam);
        return result;
    }

    @PostMapping("/delItemParam")
    @ResponseBody
    public TtResult delItemParam(@RequestParam("ids[]") Integer[] ids)
    {
        List<Integer> integers = Arrays.asList(ids.clone());
        TtResult result = itemParamService.delItemParam(integers);
        return result;
    }

    @PostMapping("/updateParam")
    @ResponseBody
    public TtResult updateParam(ItemParam param)
    {
        TtResult result = itemParamService.updateItemParam(param);
        return result;
    }

}
